<template>
    <div class="form-render-123">
        <div style="display: flex;flex-direction: column;align-items: center;">
            <div class="flex-row-title" style="color: #606266;margin-bottom: 15px">
                {{ formDataConfig ? formDataConfig.formGlobalConf?.topTitle : '问卷标题' }}
            </div>
            <div class="flex-row-title" style="color: #606266;margin-bottom: 15px">
                {{ formDataConfig ? formDataConfig.formGlobalConf?.titleTip : '' }}
            </div>


            <div class="wentizu" style="width: 100%;align-self: flex-start" v-if="formDataConfig">
                <template>
                    <div v-for="item in formDataConfig.containerConfigList" :key="item.id">
                        <!--                        问题组标题-->
                        <div class="flex-row wenti-title">
                            <div>{{ item.sortTitle }}、</div>
                            <div>{{ item.title }}</div>
                        </div>

                        <div v-if="isShowWenSource()">
                            <template v-for="(itemComp,index) in item.widgetList">
                                <component :is="itemComp.type + '-editor'"
                                           :ref="itemComp.type + '_' + itemComp.id"
                                           :childInd="index"
                                           :key="itemComp.id"
                                           :compOption="itemComp" :prentCompOption="item"
                                           @activeStartChange="startChange"
                                           style="margin-bottom: 30px" :mode="mode"
                                           :sourceMode="sourceMode"
                                           v-if="!itemComp.options.hidden"
                                           :proviceList="proviceList"
                                           @throwHidden="(bol) => getHidden(bol,itemComp)"
                                           @throwReadOnly="(readOnly) => getReadOnly(readOnly,itemComp)"></component>
                            </template>
                        </div>
                        <div v-else style="display: flex;justify-content: center">
                            <el-empty description="当前问题组暂未设置问题"></el-empty>
                        </div>
                    </div>
                </template>
            </div>
        </div>
    </div>
</template>

<script>
import {containerConfigList, formGlobalConf} from "@/components/form-designer/container-panel/containerConfig";
import refMins from "@/components/form-render/refMins";
import axios from "axios";

export default {
    name: "wenjuanRender",
    mixins: [refMins],
    props: {
        formJson: {
            type: Object,
            default: () => {
                return null
            }
        },
    },
    data() {
        return {
            formDataConfig: null,
            mode:'xiezuo',
            sourceMode:'yulan',
            proviceList:[],
        }
    },
    mounted(){
        if(this.mode != 'sheji'){
            //todo 待打开
            axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
            const instance = axios.create({
                baseURL: 'http://129.211.212.235:8095',
                timeout: 10000,
            });
            instance.get("/base/provinceCityArea/getData").then(res => {
                if (res.data.code == 200) {
                    this.proviceList = res.data.data
                } else {
                    this.proviceList = []
                }
            })
        }
    },
    methods: {
        setFormJson(json,mode1 = 'xiezuo',sourceMode1 = 'yulan'){
            const obj1 = JSON.parse(JSON.stringify(containerConfigList))
            if (!json) {
                this.formDataConfig = {
                    containerConfigList: obj1,
                    formGlobalConf
                }
            } else {
                this.formDataConfig = JSON.parse(json)
            }
            this.mode = mode1
            this.sourceMode = sourceMode1
            this.isShowWenSource()
            this.$forceUpdate()
        },
        isShowWenSource() {
            let bol = false
            for (let val of this.formDataConfig.containerConfigList) {
                if (val.widgetList.length) {
                    bol = true
                    break
                }
            }
            return bol
        },
        startChange(objSet) {
            eval(objSet.code);
        },
        getHidden(bol, item) {
            item.options.hidden = bol
        },
        getReadOnly(readOnly, item) {
            item.options.readOnly = readOnly
        },
        throwGlobalRenValue() {
            return new Promise((resolve,reject) => {
                const {containerConfigList} = this.formDataConfig
                let bol = true
                // for(let val of containerConfigList){
                //     for(let childVal of val.widgetList){
                //         //说明是必填的
                //         if(childVal.options.required == true){
                //             if(childVal.type == 'multInput'){
                //                 for(let res of childVal.options.inputConfig){
                //                     if(!res.value){
                //                         bol = false
                //                         reject('请填写'+childVal.displayName)
                //                         break
                //                     }
                //                     else{
                //                         continue
                //                     }
                //                 }
                //             }
                //             else if(childVal.type == 'markScore'){
                //                 for(let res of childVal.options.scoreConfig){
                //                     if(!res.value){
                //                         bol = false
                //                         reject('请填写'+childVal.displayName)
                //                         break
                //                     }
                //                     else{
                //                         continue
                //                     }
                //                 }
                //             }
                //             else{
                //                 if(Array.isArray(childVal.options.value)){
                //                     if(!childVal.options.value.length){
                //                         bol = false
                //                         reject('请填写'+childVal.displayName)
                //                         break
                //                     }
                //                 }
                //                 else{
                //                     if(!childVal.options.value){
                //                         bol = false
                //                         reject('请填写'+childVal.displayName)
                //                         break
                //                     }
                //                 }
                //             }
                //         }
                //         else{
                //             continue
                //         }
                //     }
                //     if(!bol) break
                // }
                if(bol) resolve(this.formDataConfig)
            })
        },
    }
}
</script>

<style scoped lang="scss">
.form-render-123 {
  width: 100%;
  height: 100%;

  .flex-row-title {
    display: flex;
    justify-content: center;
    align-items: center
  }
}

.wentizu {

}

.wenti-title {
  font-size: 15px;
  color: #444242;
  margin-bottom: 15px;
}
</style>
